﻿using Futil;
using System;
using System.Configuration;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OKDebug
{
    public partial class FrmPLCMN : Form
    {
        public PLCMN _pLCMN;
        public FrmPLCMN()
        {
            InitializeComponent();
            BuildView();
        }

        public void BuildView()
        {
            int pageIndex = 0;
            foreach (TabPage tabPage in tabControl1.TabPages)
            {
                tabPage.Controls.Clear();
                tabPage.BackColor = System.Drawing.Color.LightBlue;
                int startRowIndex = pageIndex * 15;
                for (var rowIndex = startRowIndex; rowIndex < startRowIndex + 15; rowIndex++)
                {
                    int top = (rowIndex - startRowIndex) * 25 + 5;
                    Label lbl = new Label();
                    lbl.Width = 60;
                    lbl.Left = 5;
                    lbl.Top = top;
                    lbl.Text = (rowIndex + 1) + " 名称";
                    tabPage.Controls.Add(lbl);
                    TextBox txt = new TextBox();
                    txt.Left = lbl.Left + lbl.Width + 5;
                    txt.Top = top;
                    txt.Name = "txtPLCNAME" + (rowIndex + 1);
                    tabPage.Controls.Add(txt);

                    lbl = new Label();
                    lbl.Width = 30;
                    lbl.Left = txt.Left + txt.Width + 5; ;
                    lbl.Top = top;
                    lbl.Text = "地址";
                    tabPage.Controls.Add(lbl);
                    txt = new TextBox();
                    txt.Width = 60;
                    txt.Left = lbl.Left + lbl.Width + 5;
                    txt.Top = top;
                    txt.Name = "txtPLCADDR" + (rowIndex + 1);
                    tabPage.Controls.Add(txt);

                    lbl = new Label();
                    lbl.Width = 30;
                    lbl.Left = txt.Left + txt.Width + 5;
                    lbl.Top = top;
                    lbl.Text = "长度";
                    tabPage.Controls.Add(lbl);
                    txt = new TextBox();
                    txt.Width = 50;
                    txt.Left = lbl.Left + lbl.Width + 5;
                    txt.Top = top;
                    txt.Name = "txtPLCLEN" + (rowIndex + 1);
                    tabPage.Controls.Add(txt);

                    lbl = new Label();
                    lbl.Width = 30;
                    lbl.Left = txt.Left + txt.Width + 5; ;
                    lbl.Top = top;
                    lbl.Text = "类型";
                    tabPage.Controls.Add(lbl);
                    ComboBox comboBox = new ComboBox
                    {
                        Width = 65,
                        DropDownStyle = ComboBoxStyle.DropDownList
                    };
                    comboBox.Left = lbl.Left + lbl.Width + 5;
                    comboBox.Top = top;
                    comboBox.Name = "cbxPLCTYPE" + (rowIndex + 1);
                    comboBox.Items.Add("");
                    comboBox.Items.Add("ascii");
                    comboBox.Items.Add("unicode");
                    comboBox.Items.Add("short");
                    comboBox.Items.Add("int");
                    comboBox.Items.Add("float");
                    comboBox.Items.Add("byte");
                    tabPage.Controls.Add(comboBox);

                    lbl = new Label();
                    lbl.Width = 20;
                    lbl.Left = comboBox.Left + comboBox.Width + 5;
                    lbl.Top = top;
                    lbl.Text = "值";
                    tabPage.Controls.Add(lbl);
                    txt = new TextBox();
                    txt.Left = lbl.Left + lbl.Width + 5;
                    txt.Top = top;
                    txt.Name = "txtPLCVAL" + (rowIndex + 1);
                    tabPage.Controls.Add(txt);

                    Button btnadd = new Button();
                    btnadd.Name = "btnADD" + (rowIndex + 1);
                    btnadd.Tag = rowIndex;
                    btnadd.Width = 50;
                    btnadd.Text = "追加";
                    btnadd.Left = txt.Left + txt.Width + 2;
                    btnadd.Top = top;
                    btnadd.Click += btnAdd_Click;
                    tabPage.Controls.Add(btnadd);

                    Button btndel = new Button();
                    btndel.Tag = rowIndex;
                    btndel.Name = "btnDEL" + (rowIndex + 1);
                    btndel.Width = 50;
                    btndel.Text = "删除";
                    btndel.Left = btnadd.Left + btnadd.Width + 2;
                    btndel.Top = top;
                    btndel.Click += btnDel_Click;
                    tabPage.Controls.Add(btndel);
                }

                pageIndex++;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            FrmPLCMNAdd frmPLCMNAdd = new FrmPLCMNAdd();
            frmPLCMNAdd._rowIndex = Convert.ToInt32(btn.Tag.ToString());
            frmPLCMNAdd._pLCMN = _pLCMN;
            frmPLCMNAdd.OnOK += OnOK;
            frmPLCMNAdd.ShowDialog();
        }

        private void OnOK(object sender, EventArgs e)
        {
            SaveData();
            BuildView();
            DisplayData();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            DialogResult result = MessageBox.Show("确认删除吗？", "确认对话框",MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                _pLCMN.PLCAddrValList.RemoveAt(Convert.ToInt32(btn.Tag.ToString()));
                OnOK(null,null);
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            SaveViewData();
            MessageBox.Show("更新完成！");
        }

        private void FrmPLCMN_Load(object sender, EventArgs e)
        {
            DisplayData();
        }

        public void SaveViewData()
        {
            _pLCMN.BIGMODE = cbxPLCMODE.Checked;
            Task.Factory.StartNew(() =>
            {
                lock (_pLCMN.PLCAddrValList)
                {
                    _pLCMN.PLCAddrValList.Clear();
                    int count = tabControl1.TabPages.Count * 15;
                    for (var i = 0; i < count; i++)
                    {
                        PLCAddrVal pLCAddrVal = new PLCAddrVal();
                        Control ctrl = Common.FindControl(tabControl1, "txtPLCNAME" + (i + 1));
                        if (ctrl != null)
                        {
                            pLCAddrVal.Name = ctrl.Text.Trim();
                        }

                        ctrl = Common.FindControl(tabControl1, "txtPLCADDR" + (i + 1));
                        if (ctrl != null)
                        {
                            pLCAddrVal.Addr = ctrl.Text.Trim();
                        }

                        ctrl = Common.FindControl(tabControl1, "cbxPLCTYPE" + (i + 1));
                        if (ctrl != null)
                        {
                            ComboBox comboBox = (ComboBox)ctrl;
                            if (comboBox.SelectedItem != null)
                            {
                                pLCAddrVal.Type = comboBox.SelectedItem.ToString();
                            }
                        }

                        ctrl = Common.FindControl(tabControl1, "txtPLCLEN" + (i + 1));
                        if (ctrl != null)
                        {
                            switch (pLCAddrVal.Type)
                            {
                                case "ascii": pLCAddrVal.Length = ctrl.Text.Trim(); break;
                                case "unicode": pLCAddrVal.Length = ctrl.Text.Trim(); break;
                                case "short": pLCAddrVal.Length = "1"; break;
                                case "int": pLCAddrVal.Length = "2"; break;
                                case "float": pLCAddrVal.Length = "2"; break;
                                case "byte": pLCAddrVal.Length = "0.5"; break;
                            }
                        }

                        ctrl = Common.FindControl(tabControl1, "txtPLCVAL" + (i + 1));
                        if (ctrl != null)
                        {
                            pLCAddrVal.Val = ctrl.Text.Trim();
                        }

                        _pLCMN.PLCAddrValList.Add(pLCAddrVal);
                    }

                    SaveData();
                }
            });
        }

        public void SaveData()
        {
            string plc_mn_name = string.Empty;
            string plc_mn_address = string.Empty;
            string plc_mn_length = string.Empty;
            string plc_mn_type = string.Empty;
            string plc_mn_value = string.Empty;
            foreach (var plcAddrVal in _pLCMN.PLCAddrValList)
            {
                if (string.IsNullOrEmpty(plc_mn_address))
                {
                    plc_mn_name = plcAddrVal.Name;
                    plc_mn_address = plcAddrVal.Addr;
                    plc_mn_length = plcAddrVal.Length;
                    plc_mn_type = plcAddrVal.Type;
                    plc_mn_value = plcAddrVal.Val;
                }
                else
                {
                    plc_mn_name += "," + plcAddrVal.Name;
                    plc_mn_address += "," + plcAddrVal.Addr;
                    plc_mn_length += "," + plcAddrVal.Length;
                    plc_mn_type += "," + plcAddrVal.Type;
                    plc_mn_value += "," + plcAddrVal.Val;
                }
            }

            XmlHelper.SaveNodeAttribute(System.Windows.Forms.Application.ExecutablePath + ".config", "configuration/appSettings/add", "key", "plc_mn_name", "value", plc_mn_name);
            ConfigurationManager.RefreshSection("appSettings");
            XmlHelper.SaveNodeAttribute(System.Windows.Forms.Application.ExecutablePath + ".config", "configuration/appSettings/add", "key", "plc_mn_address", "value", plc_mn_address);
            ConfigurationManager.RefreshSection("appSettings");
            XmlHelper.SaveNodeAttribute(System.Windows.Forms.Application.ExecutablePath + ".config", "configuration/appSettings/add", "key", "plc_mn_length", "value", plc_mn_length);
            ConfigurationManager.RefreshSection("appSettings");
            XmlHelper.SaveNodeAttribute(System.Windows.Forms.Application.ExecutablePath + ".config", "configuration/appSettings/add", "key", "plc_mn_type", "value", plc_mn_type);
            ConfigurationManager.RefreshSection("appSettings");
            XmlHelper.SaveNodeAttribute(System.Windows.Forms.Application.ExecutablePath + ".config", "configuration/appSettings/add", "key", "plc_mn_value", "value", plc_mn_value);
            ConfigurationManager.RefreshSection("appSettings");
            XmlHelper.SaveNodeAttribute(System.Windows.Forms.Application.ExecutablePath + ".config", "configuration/appSettings/add", "key", "plc_mn_bigmode", "value", cbxPLCMODE.Checked?"TRUE":"FALSE");
            ConfigurationManager.RefreshSection("appSettings");
        }

        public void DisplayData()
        {
            cbxPLCMODE.Checked = _pLCMN.BIGMODE;
            for (var i=0;i< _pLCMN.PLCAddrValList.Count; i++)
            {
                Control ctrl=Common.FindControl(tabControl1, "txtPLCNAME" + (i+1));
                if (ctrl != null)
                {
                    ctrl.Text = _pLCMN.PLCAddrValList[i].Name;
                }

                ctrl = Common.FindControl(tabControl1, "txtPLCADDR" + (i + 1));
                if (ctrl != null)
                {
                    ctrl.Text = _pLCMN.PLCAddrValList[i].Addr;
                }

                ctrl = Common.FindControl(tabControl1, "txtPLCLEN" + (i + 1));
                if (ctrl != null)
                {
                    ctrl.Text = _pLCMN.PLCAddrValList[i].Length;
                }

                ctrl = Common.FindControl(tabControl1, "cbxPLCTYPE" + (i + 1));
                if (ctrl != null)
                {
                    ComboBox comboBox = (ComboBox)ctrl;
                    comboBox.SelectedItem = _pLCMN.PLCAddrValList[i].Type;
                }

                ctrl = Common.FindControl(tabControl1, "txtPLCVAL" + (i + 1));
                if (ctrl != null)
                {
                    ctrl.Text = _pLCMN.PLCAddrValList[i].Val;
                }
            }
        }

        private void btnClearVal_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("确认清除数值吗？", "确认对话框", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                foreach (var plcAddrVal in _pLCMN.PLCAddrValList)
                {
                    plcAddrVal.Val = string.Empty;
                }

                XmlHelper.SaveNodeAttribute(System.Windows.Forms.Application.ExecutablePath + ".config", "configuration/appSettings/add", "key", "plc_mn_value", "value", string.Empty);
                ConfigurationManager.RefreshSection("appSettings");
                OnOK(null, null);
            }
        }

        private void btnShowDescription_Click(object sender, EventArgs e)
        {
            FrmShowDescription frmShowDescription = new FrmShowDescription();
            frmShowDescription.ShowDialog();
        }
    }
}
